 //
//  BGAttentionBasicViewController.m
//  bigMall
//
//  Created by ios on 16/7/19.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGAttentionBasicViewController.h"
#import "BGAttentionViewController.h"
#import "BGDateResult.h"
#import "BGBGOwnCollectListResultModel.h"
#import "BGGuanZhuCell.h"
#import "BGOwnCollectListModel.h"
#import "BGCheckCollect.h"
#import "BGGoodInfoViewController.h"
#import "BGTenderInfoViewController.h"
#import "BGAuctionInfoViewController.h"

@interface BGAttentionBasicViewController () {
    
    NSMutableArray *_dataArr;
    int index;
    UIImageView *imageView;
    UILabel *lable;
    
}

@end

@implementation BGAttentionBasicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    _dataArr = [NSMutableArray array];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, BGWidth, BGHight + 100) style:UITableViewStyleGrouped];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    //刷新的加载
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(index)];
    
    [self.tableView.header beginRefreshing];
    
    [self initDat];
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake((BGWidth - 70 ) / 2 + 10, (self.tableView.height - 109 - 160) / 3, 70, 90)];
    imageView.image = [UIImage imageNamed:@"抱歉~暂无数据"];
    
    lable = [[UILabel alloc] initWithFrame:CGRectMake((BGWidth - 120 ) / 2 + 10, imageView.bottom + 21, 120, 16)];
    lable.text = @"抱歉~暂无数据";
    lable.font = [UIFont systemFontOfSize:16];
    [self.tableView addSubview:lable];
    
    [self.tableView addSubview:imageView];
    imageView.hidden = YES;
    lable.hidden = YES;

}

- (void)addIndex {
    
    index++;
    [self loadInter];
    
}
- (void)index {
    
    index = 1;
    [_dataArr removeAllObjects];
    [self loadInter];
    
}

- (void)initDat {
    
    _dataArr = [NSMutableArray array];
    
}

- (void)loadInter {
    
    [BGDateResult ownCollectListWithThisPage:[NSString stringWithFormat:@"%d",index] pageNum:@"10" type:self.type success:^(NSObject *result) {
        
        BGBGOwnCollectListResultModel *model = [[BGBGOwnCollectListResultModel alloc] init];
        model = (BGBGOwnCollectListResultModel *)result;
        NSArray *listArr = [NSArray arrayWithArray:model.list];
        for (BGOwnCollectListModel *model in listArr) {
            [_dataArr addObject:model];
        }
//        _dataArr = listArr;
        if (_dataArr.count == 0 && index == 1) {
            
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshingWithNoMoreData];
            
            //判断数据源有无
            
          
            imageView.hidden = NO;
            lable.hidden = NO;
            
        }
        if ([model.allPage intValue] > index) {
            self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addIndex)];
        }
        if (listArr.count == 0) {
            [self.tableView.footer endRefreshingWithNoMoreData];
            return;
        }
        
        imageView.hidden = YES;
        lable.hidden = YES;
        [self.tableView.footer endRefreshing];
        [self.tableView.header endRefreshing];
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 111;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (_dataArr.count != 0) {
        
        return _dataArr.count;
        
    } else {
        return 0;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView registerNib:[UINib nibWithNibName:@"BGGuanZhuCell" bundle:nil] forCellReuseIdentifier:@"celId"];
    BGGuanZhuCell *cel = [tableView dequeueReusableCellWithIdentifier:@"celId" forIndexPath:indexPath];
    
    BGOwnCollectListModel *model = [[BGOwnCollectListModel alloc] init];
    
    if (_dataArr.count != 0) {
        
       model = _dataArr[indexPath.row];
        cel.zhonglei.text = model.cat_id;
        cel.biaoti.text = model.title;
        cel.price.text = [NSString stringWithFormat:@"%@元/吨",model.price];
        cel.number.text = [NSString stringWithFormat:@"数量：%@吨",model.number];
        cel.jiaohuodi.text = model.delivery_area;
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:[model.end_time intValue]];
        NSDateFormatter *fat = [[NSDateFormatter alloc] init];
        [fat setDateFormat:@"yyyy-MM-dd"];
        NSString *str = [fat stringFromDate:date];
        cel.jiezhishijian.text = str;
        
        if ([model.is_ported isEqual:@"0"]) {
            cel.kepindan.image = [UIImage imageNamed:@""];
        }else {
            cel.kepindan.image = [UIImage imageNamed:@"可拼单"];
        }
        
        if ([self.type isEqualToString:@"auction"]) {
            cel.kepindan.image = [UIImage imageNamed:@""];
        }
        
        if ([model.publish_type isEqualToString:@"0"]) {
            cel.guadanfangshi.image = [UIImage imageNamed:@"仓单挂单"];
        }else if ([model.publish_type isEqual:@"1"]) {
            cel.guadanfangshi.image = [UIImage imageNamed:@"保证金挂单"];
        }else {
            cel.guadanfangshi.image = [UIImage imageNamed:@"信誉挂单-拷贝"];
        }
        
        
        
        
    }
    // Configure the cell...
    
    return cel;
}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"取消关注" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        //网络的删除
        BGOwnCollectListModel *model = [[BGOwnCollectListModel alloc] init];
        
        if (_dataArr.count != 0) {
            
            model = _dataArr[indexPath.row];
            
        }
        [BGDateResult cancelCollectWithId:model.collect_id success:^(NSObject *result) {
            
            BGCheckCollect *a = [[BGCheckCollect alloc] init];
            a = (BGCheckCollect *)result;
            if ([a.status isEqualToString:@"1"]) {
                
                [self showMessage:a.msg];
                
            }else {
                
                [self showMessage:a.msg];
                
            }
            
        } failure:^(NSError *error) {
            
        }];
        
        //数组的删除
        NSArray *indexArr = @[indexPath];
        [_dataArr removeObjectAtIndex:indexPath.row];
        
        //tableview 行删除
        [tableView deleteRowsAtIndexPaths:indexArr withRowAnimation:UITableViewRowAnimationLeft];
        
        
    }];
    
    return @[delete];
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    BGGoodInfoViewController *a = [[BGGoodInfoViewController alloc] init];
    BGTenderInfoViewController *b = [[BGTenderInfoViewController alloc] init];
    BGAuctionInfoViewController *c = [[BGAuctionInfoViewController alloc] init];
    BGOwnCollectListModel *model = [[BGOwnCollectListModel alloc] init];
    a.title = @"商品详情";
    
    if (_dataArr.count != 0) {
        
        model = _dataArr[indexPath.row];
        
        if ([model.status isEqualToString:@"1"]) {
            
            if ([self.type isEqualToString:@"goods"]) {
                a.goodId = model.obj_id;
                self.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:a animated:YES];
            } else if ([self.type isEqualToString:@"invite"]) {
                b.goodId = model.obj_id;
                self.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:b animated:YES];
            } else {
                c.goodId = model.obj_id;
                self.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:c animated:YES];
            }
            
            
        }else {
            NSString *statusStr;
            switch ([model.status intValue]) {
                case -1:
                    statusStr = @"该商品准备资料，不可查看";
                    break;
                case 0:
                    statusStr = @"该商品待审核，不可查看";
                    break;
                case 2:
                    statusStr = @"该商品审核失败，不可查看";
                    break;
                case 3:
                    statusStr = @"该商品下架，不可查看";
                    break;
                case 4:
                    statusStr = @"该商品售空，不可查看";
                    break;
                default:
                    break;
            }
            
            [self showMessage:statusStr];
            
        }
        
    }
  
}

- (void)showMessage:(NSString *)message {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
